// ==UserScript==
// @name        YA-Fandom-Debloater
// @namespace   Violentmonkey Scripts
// @match       https://*.fandom.com/*
// @grant       none
// @version     1.5
// @author      Steve0Greatness
// @description (Yet Another Fandom Debloater)
// @copyright   GPL-v3
// @homepageURL  https://notabug.org/Steve0Greatness/Yet-Another-Fandom-Debloater
// @supportURL  https://notabug.org/Steve0Greatness/Yet-Another-Fandom-Debloater/issues
// @updateURL   https://notabug.org/Steve0Greatness/Yet-Another-Fandom-Debloater/raw/master/fandom-debloater.user.js
// ==/UserScript==


if (location.search == "?action=edit&redlink=1") location.replace(location.pathname);

`<div class="sticky-toc-wrapper" aria-labelledby="mw-sticky-toc-heading"><button class="page-side-tool sticky-toc-toggle" data-wds-tooltip="Contents" data-wds-tooltip-position="right" data-tooltip-attached="1" aria-label="Contents"><svg class="wds-icon wds-icon-small toc-small-icon">
<use href="#wds-icons-bullet-list-small"></use>
</svg></button><div id="sticky-toc" role="navigation" class="toc sticky-toc"><div class="toctitle" lang="en" dir="ltr"><h2 id="mw-sticky-toc-heading">Contents</h2><svg class="wds-icon wds-icon-tiny sticky-toc-close">
<use href="#wds-icons-close-tiny"></use>
</svg></div><ul></ul>
</div></div>`

waitForElementToDisplay("#p-tb .rail-module__list", function() {
  const PageTools = document.querySelectorAll("#p-tb .rail-module__list :any-link");
  const toolsWrapper = buildToolsButton();
  for (let i = 0; i < PageTools.length; i++) {
    toolsWrapper.querySelector("ul").innerHTML += `<li><a href="${PageTools[i].href}">${PageTools[i].innerText}</a></li>`;
  }
  document.querySelector(".page-side-tools").appendChild(toolsWrapper)
}, 500, 2000);

function buildToolsButton() {
  const StickyToolsWrapper = document.createElement("div");
  StickyToolsWrapper.classList.add("sticky-toc-wrapper")
  StickyToolsWrapper.setAttribute("aria-labelledby", "mw-sticky-tools-heading")
  StickyToolsWrapper.innerHTML = `<button class="page-side-tool sticky-toc-toggle" data-wds-tooltip="Page Tools" data-wds-tooltip-position="right" data-tooltip-attached="1" aria-label="Page Tools">
<svg xmlns="http://www.w3.org/2000/svg" width="18" height="18" fill="none" viewBox="0 0 24 24" class="wds-icon wds-icon-small toc-small-icon">
	<g clip-path="url(#wrench_clip0)">
		<path d="M23 4c-.265 0-.52.105-.707.293C22.105 4.48 22 4.735 22 5v2h-5V2h2c.265 0 .52-.105.707-.293C19.895 1.52 20 1.265 20 1c0-.265-.105-.52-.293-.707C19.52.105 19.265 0 19 0h-5c-.132 0-.262.024-.384.074-.122.05-.233.123-.326.216l-4 4c-.093.093-.166.204-.216.326C9.024 4.738 9 4.868 9 5v4.59l-7.71 7.7C.571 18.009.167 18.984.167 20c0 1.017.404 1.991 1.123 2.71.719.719 1.694 1.122 2.71 1.122s1.991-.403 2.71-1.122l7.7-7.71H19c.132 0 .262-.024.384-.074.122-.05.233-.123.326-.216l4-4c.093-.093.166-.204.216-.326.05-.122.075-.252.074-.384V5c0-.265-.105-.52-.293-.707C23.52 4.105 23.265 4 23 4zm-1 5.59L18.59 13H14c-.132 0-.262.024-.384.074-.122.05-.233.123-.326.216l-8 8c-.342.342-.806.534-1.29.534s-.948-.192-1.29-.534c-.342-.342-.534-.806-.534-1.29s.192-.948.534-1.29l8-8c.093-.093.166-.204.216-.326.05-.122.075-.252.074-.384V5.41L14.41 2H15v6c0 .265.105.52.293.707.187.188.442.293.707.293h6v.59z"></path>
	</g>
	<defs>
		<clipPath id="wrench_clip0">
			<path fill="#fff" d="M0 0H24V24H0z"></path>
		</clipPath>
	</defs>
</svg>
</button><div id="sticky-tools" role="navigation" class="toc sticky-toc wds-is-hidden"><div class="toctitle" lang="en" dir="ltr"><h2 id="mw-sticky-tools-heading">Page Tools</h2><svg id="tools-close-button" class="wds-icon wds-icon-tiny sticky-toc-close">
<use href="#wds-icons-close-tiny"></use>
  </svg></div><ul></ul></div>`
  StickyToolsWrapper.querySelector("button").addEventListener("click", function() {
    StickyToolsWrapper.querySelector("#sticky-tools").classList.toggle("wds-is-hidden");
  });
  StickyToolsWrapper.querySelector("#tools-close-button").addEventListener("click", function() {
    if (!StickyToolsWrapper.querySelector("#sticky-tools").classList.contains("wds-is-hidden")) StickyToolsWrapper.querySelector("#sticky-tools").classList.add("wds-is-hidden");
  });
  return StickyToolsWrapper;
}

function waitForElementToDisplay(selector, callback, checkFrequencyInMs, timeoutInMs, stopAfterLoops=-1) {
  var startTimeInMs = Date.now();
  var index = 0;
  (function loopSearch() {
    if (document.querySelector(selector) != null) {
      console.debug(`Found ${selector}!`)
      callback();
      return;
    }
    setTimeout(function () {
      if (timeoutInMs && Date.now() - startTimeInMs > timeoutInMs)
        return;
      loopSearch();
    }, checkFrequencyInMs);
  })();
}
